package com.sky.business.sys.handler.endpoint;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.sky.business.sys.domain.SystemConfig;
import com.sky.business.sys.event.AiStatus;
import com.sky.business.sys.event.CmccBeginTrackingReportEvent;
import com.sky.business.sys.handler.MqttEventHandler;
import com.sky.business.sys.service.SystemConfigService;
import com.sky.business.sys.util.MQTTClientUtils;
import com.sky.common.config.CacheConstants;
import com.sky.common.track.AlarmManager;
import com.sky.common.track.TrackTaskInfo;
import com.sky.common.track.TrackTaskManager;
import com.sky.common.utils.MapUtil;
import com.sky.common.utils.NumberUtil;
import com.sky.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.SimpleTimeZone;

/**
 * 终端响应开始跟踪事件
 */
@Component
@Slf4j
public class BeginTrackingReportEventHandler implements MqttEventHandler {

    public static final ObjectMapper objectMapper = new ObjectMapper().setTimeZone(SimpleTimeZone.getTimeZone("GMT+8"));

    @Autowired
    private SystemConfigService systemConfigService;

    @Override
    public boolean match(String topic, String msg, String event) {
        return "BeginTracking".equals(event);
    }

    @Override
    public void handle(String topic, String msg) throws Exception {
        SystemConfig systemConfig = systemConfigService.getSystemConfig();
        CmccBeginTrackingReportEvent cmccBeginTrackingReportEvent = objectMapper.readValue(msg, CmccBeginTrackingReportEvent.class);
        CmccBeginTrackingReportEvent.Metadata metadata = cmccBeginTrackingReportEvent.getMetadata();
        AiStatus aiStatus = metadata.getAiStatus();
        if (metadata.getWorkState() == CacheConstants.BEGIN_TRACKING_WORK_STATE && StringUtils.isNotEmpty(aiStatus.getClassName()) &&
                aiStatus.getDetectConfidence() != null && aiStatus.getDetectConfidence().doubleValue() >= CacheConstants.DETECT_CONFIDENCE) {
            log.warn("设备已追踪到无人机,设备ID:{},className:{},DetectConfidence:{},TrackConfidence:{}", metadata.getDeviceId(), aiStatus.getClassName(), aiStatus.getDetectConfidence(), aiStatus.getTrackConfidence());
            AlarmManager.add(metadata.getTaskId(),metadata.getDeviceId(),aiStatus.getClassName(),false);
            TrackTaskManager.updateTrackedTime(metadata.getTaskId(), metadata.getDeviceId());
        }
        //增加附加信息
        if (StringUtils.isNotEmpty(metadata.getTaskId())) {
            TrackTaskInfo trackTaskInfo = TrackTaskManager.getTrackTaskInfo(metadata.getTaskId());
            if (trackTaskInfo != null && trackTaskInfo.getLastBeginTrackEvent() != null && trackTaskInfo.getLastBeginTrackEvent().getMetadata() != null) {
                Map lastExtention = trackTaskInfo.getLastBeginTrackEvent().getMetadata().getExtention();
                if (lastExtention != null) {
                    Map extention = cmccBeginTrackingReportEvent.getMetadata().getExtention();
                    if (extention == null) {
                        extention = new HashMap();
                        cmccBeginTrackingReportEvent.getMetadata().setExtention(extention);
                    }
                    MapUtil.sync(lastExtention,extention);
                }
            }
        }
        String json = objectMapper.writeValueAsString(cmccBeginTrackingReportEvent);
        MQTTClientUtils.publish(MQTTClientUtils.CMCC_MQTT_CLIENT, systemConfig.getCmccReportingTopic(), CacheConstants.DEFAULT_QOS, json);
    }

}
